﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace Tanks
{
    public class Graph
    {
        public int Resolution = 72;
        private Bitmap canvas;
        int fieldWidth;
        int fieldHeight;
        Game game=Game.Instance();
        Cell[,] Grid;
        //Bitmaps
        private Bitmap destructibleB = (Bitmap)Bitmap.FromFile("Destructible.bmp", true);
        private Bitmap emptyB = (Bitmap)Bitmap.FromFile("Empty.bmp", true);
        private Bitmap solidB = (Bitmap)Bitmap.FromFile("Solid.bmp", true);
        //======
        int cellSize;
        public Graph(Bitmap c, Cell[,] g, int w, int h)
        {
            cellSize  = game.CellSize;
            canvas = c;
            fieldWidth = w;
            fieldHeight = h;
            Grid = g;
            destructibleB.SetResolution(game.Resolution, game.Resolution);
            solidB.SetResolution(game.Resolution, game.Resolution);
            emptyB.SetResolution(game.Resolution, game.Resolution);
        }
        public void redraw()
        {
            drawMap();
        }
        void drawMap()
        {
            
            for (int i = 0; i < fieldWidth; i++)
            {
                for (int j = 0; j < fieldHeight; j++)
                {
                    using (Graphics gr = Graphics.FromImage(canvas))
                    {
                        switch (Grid[i, j].Block)
                        {
                            case BlockType.Destructible:
                                //gr.FillRectangle(new SolidBrush(Color.Maroon), new RectangleF(i * cellSize, j * cellSize, cellSize,cellSize));
                                gr.DrawImage(destructibleB, new Point(i * cellSize, j * cellSize));
                                break;
                            case BlockType.Solid:
                                //gr.FillRectangle(new SolidBrush(Color.Gray), new RectangleF(i * cellSize, j * cellSize,cellSize,cellSize));
                                gr.DrawImage(solidB, new Point(i * cellSize, j * cellSize));
                                break;
                            case BlockType.Empty:
                                //gr.FillRectangle(new SolidBrush(Color.DarkGreen), new RectangleF(i * cellSize, j * cellSize, cellSize, cellSize));
                                gr.DrawImage(emptyB, new Point(i * cellSize, j * cellSize));
                                if (Grid[i, j].currTank != null)
                                {
                                    Grid[i, j].currTank.Draw(canvas);
                                }
                                break;
                        }
                    }
                }
                
            }
            for (int i = 0; i < game.Expl.Count; i++)
            {
                game.Expl[i].Draw(canvas);

            }
            for (int i = 0; i < game.Miss.Count; i++)
            {
                game.Miss[i].Draw(canvas);
            }
            for (int i = 0; i < game.Items.Count; i++)
            {
                game.Items[i].Draw(canvas);
            }
        }
    }
}
